#define _CRT_SECURE_NO_WARNINGS
#include "List.h"
ListNode* BuyNode(LTDataType x)
{
	ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
	newnode->data = x;
	newnode->next = newnode->prev = newnode;
	return newnode;
}
void ListPrint(ListNode* pHead)
{
	ListNode* p = pHead->next;
	while (p!=pHead)
	{
		printf("%d->", p->data);
		p = p->next;
	}
}
ListNode* ListCreate()
{
	return BuyNode(-1);
}

void ListPushBack(ListNode* pHead, LTDataType x)
{
	assert(pHead);
	ListNode* newnode = BuyNode(x);
	newnode->next = pHead;
	newnode->prev = pHead->prev;
	pHead->prev->next = newnode;
	pHead->prev = newnode;
}

void ListPushFront(ListNode* pHead, LTDataType x)
{
	assert(pHead);
	ListNode* newnode = BuyNode(x);
	newnode->prev = pHead;
	newnode->next = pHead->next;
	pHead->next->prev = newnode;
	pHead->next = newnode;
}

bool LTEmpty(ListNode* pHead)
{
	assert(pHead);
	return pHead->next == pHead;
}
void ListPopBack(ListNode* pHead)
{
	assert(pHead);
	assert(!LTEmpty);
	ListNode* del = pHead->prev;
	pHead->prev =del->prev;
	pHead->prev->prev->next = pHead;
	free(del);
	del = NULL;
}

void ListPopFront(ListNode* pHead)
{
	assert(pHead);
	assert(!LTEmpty);
	ListNode* del = pHead->next;
	pHead->next = del->next;
	del->next->prev = pHead;
	free(del);
	del = NULL;
}
ListNode* ListFind(ListNode* pHead, LTDataType x)
{
	ListNode* pcur = pHead->next;
	while (pcur!=pHead)
	{
		if (pcur->data == x)
			return pcur;
		pcur = pcur->next;
	}
}
void ListInsert(ListNode* pos, LTDataType x)
{
	assert(pos);
	ListNode* newnode = BuyNode(x);
	newnode->next = pos;
	newnode->prev = pos->prev;
	pos->prev->next = newnode;
	pos->prev = newnode;
}

void ListErase(ListNode* pos)
{
	assert(pos);
	pos->next->prev = pos->prev;
	pos->prev->next = pos->next;
	free(pos);
	pos = NULL;
}